<template>
  <div class="login-container">
    <!-- Logo（请替换为实际图片路径） -->


    <img src="../assets/logo.png" alt="系统Logo" class="logo" />
    <h2 class="title">Login</h2>

    <!-- 登录表单 -->
    <el-form :model="form" class="login-form" v-if="!isRegister">
      <!-- 手机号/用户号输入 -->
      <el-form-item class="input">
        <el-input
          v-model="form.username"
          placeholder="请输入手机号/用户号"
        >
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>

      <!-- 密码输入（支持显示/隐藏） -->
      <el-form-item>
        <el-input
          v-model="form.password"
          :type="showPassword ? 'text' : 'password'"
          placeholder="请输入密码"
        >
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
          <template #suffix>
            <el-icon @click="showPassword = !showPassword" style="cursor: pointer;">
              <View v-if="!showPassword" />
              <Hide v-else />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>

      <!-- 记住密码 & 忘记密码 -->
      <div class="remember-row">
        <el-checkbox v-model="form.remember">记住密码</el-checkbox>
        <a href="#" class="forgot-link">忘记密码？</a>
      </div>

      <!-- 登录按钮 -->
      <el-form-item>
        <el-button
          type="primary"
          block
          @click="handleLogin"
        >
          登录
        </el-button>
      </el-form-item>

      <!-- 注册引导 -->
      <div class="register-tip">
        还没有账号？ 
        <el-link type="primary" @click="isRegister = true">立即注册</el-link>
      </div>
    </el-form>

    <!-- 注册表单 -->
    <el-form :model="form" class="login-form" v-if="isRegister">
      <!-- 手机号输入 -->
      <el-form-item>
        <el-input
          v-model="form.phone"
          placeholder="请输入手机号"
        >
          <template #prefix>
            <el-icon><Iphone /></el-icon>
          </template>
        </el-input>
      </el-form-item>

      <!-- 验证码行 -->
      <div class="captcha-row">
        <el-form-item style="flex:1;margin-right:10px">
          <el-input
            v-model="form.captcha"
            placeholder="请输入验证码"
          >
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-button
          :disabled="countdown > 0"
          @click="handleGetCaptcha"
        >
          {{ countdown > 0 ? `${countdown}秒后获取` : '获取验证码' }}
        </el-button>
      </div>

      <!-- 用户名输入 -->
      <el-form-item>
        <el-input
          v-model="form.nickname"
          placeholder="请输入用户名"
        >
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>

      <!-- 密码输入 -->
      <el-form-item>
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
        >
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>

      <!-- 注册按钮 -->
      <el-form-item>
        <el-button
          type="primary"
          block
          @click="handleRegister"
        >
          立即注册
        </el-button>
      </el-form-item>

      <!-- 登录引导 -->
      <div class="register-tip">
        已有账号？ 
        <el-link type="primary" @click="isRegister = false">立即登录</el-link>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, View, Hide, Iphone, Message } from '@element-plus/icons-vue'

// 控制注册/登录状态
const isRegister = ref(false)

// 表单数据
const form = reactive({
  username: '',  // 手机号/用户号
  password: '',  // 密码
  remember: false, // 记住密码
  phone: '',     // 注册手机号
  captcha: '',   // 验证码
  nickname: ''   // 用户名
})

// 控制密码显示/隐藏
const showPassword = ref(false)

// 验证码倒计时
const countdown = ref(0)
let timer = null

const router = useRouter()

// 登录逻辑
const handleLogin = () => {
  if (form.username === 'admin' && form.password === '123456') {
    console.log('登录成功')
    localStorage.setItem('isLoggedIn', 'true')
    localStorage.setItem('username', form.username)
    ElMessage.success('登录成功')
    router.push('/main')
  } else {
    ElMessage.error('账号或密码错误')
  }
}

// 注册逻辑
const handleRegister = async () => {
  try {
    console.log('注册信息：', {
      phone: form.phone,
      captcha: form.captcha,
      nickname: form.nickname,
      password: form.password
    })
    // 这里可扩展注册接口调用
    ElMessage.success('注册成功')
    isRegister.value = false
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error('注册失败')
  }
}

// 获取验证码
const handleGetCaptcha = () => {
  if (!form.phone) {
    ElMessage.warning('请输入手机号')
    return
  }
  countdown.value = 60
  timer = setInterval(() => {
    if (countdown.value <= 0) {
      clearInterval(timer)
      return
    }
    countdown.value--
  }, 1000)
  ElMessage.success('验证码已发送')
  console.log('已发送验证码到:', form.phone)
}
</script>

<style scoped>
/* 移动端登录页面样式 */
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #ffffff;
  padding: 20px;
  position: relative;
}

/* Logo 样式 */
.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
  z-index: 1;
  position: relative;
}

/* 标题样式 */
.title {
  font-size: 20px;
  margin-bottom: 24px;
  font-weight: 600;
  color: #333333;
  z-index: 1;
  position: relative;
}

/* 表单容器样式 */
.login-form {
  width: 100%;
  max-width: 320px;
  background: #ffffff;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid #f0f0f0;
  z-index: 1;
  position: relative;
  transition: all 0.3s ease;
}

.login-form:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

/* 表单项样式 */
.el-form-item {
  margin-bottom: 16px;
}

/* 输入框样式 */
.el-input {
  --el-input-height: 44px;
  --el-input-border-radius: 8px;
}

.el-input__wrapper {
  border-radius: 8px;
  box-shadow: 0 0 0 1px #e4e7ed;
  background: #ffffff;
  transition: all 0.3s ease;
}

.el-input__wrapper:hover {
  box-shadow: 0 0 0 1px #409eff;
}

.el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #409eff;
}

/* 按钮样式 */
.el-button {
  width: 100%;
  height: 44px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  background: #409eff;
  border: none;
  transition: all 0.3s ease;
}

.el-button:hover {
  background: #36a3f7;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

/* 记住密码行布局 */
.remember-row, .captcha-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.captcha-row {
  gap: 8px;
}

.captcha-row .el-form-item {
  flex: 1;
  margin-bottom: 0;
}

.captcha-row button {
  width: 100px;
  height: 44px;
  border-radius: 8px;
  font-size: 14px;
  background: #67c23a;
  border: none;
  transition: all 0.3s ease;
}

.captcha-row button:hover:not(:disabled) {
  background: #5daf34;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(103, 194, 58, 0.3);
}

.captcha-row button:disabled {
  background: #c0c4cc;
  cursor: not-allowed;
}

/* 忘记密码链接样式 */
.forgot-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #36a3f7;
}

/* 注册提示样式 */
.register-tip {
  margin-top: 16px;
  text-align: center;
  color: #606266;
  font-size: 14px;
}

/* 移动端适配 */
@media screen and (max-width: 375px) {
  .login-container {
    padding: 16px;
  }
  
  .login-form {
    padding: 20px;
    border-radius: 10px;
  }
  
  .logo {
    width: 70px;
    height: 70px;
  }
  
  .title {
    font-size: 18px;
  }
}

@media screen and (max-width: 320px) {
  .login-container {
    padding: 12px;
  }
  
  .login-form {
    padding: 16px;
    border-radius: 8px;
  }
  
  .logo {
    width: 60px;
    height: 60px;
  }
  
  .title {
    font-size: 16px;
  }
}
</style> 